package models.data;

import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

//import sun.misc.IOUtils;

/**
 * Created with IntelliJ IDEA.
 * User: olgak
 * Date: 29.3.2013
 * Time: 14:32
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Vidhengitafla extends Model {

    @Id
    public int vidhengi_id;

    @Constraints.Required
    public int spurning_id;

    @Constraints.Required
    public String nafn;

    public byte[] vidhengi;
    public Date dagur_buidtil;
    public String kennari_id_buidtil;
    public String contenttype;

    public Vidhengitafla(int spurning_id, String nafn, byte[] vidhengi, String kennari_id, Date date, String contenttype) {
        this.vidhengi_id = createId();
        this.spurning_id = spurning_id;
        this.nafn = nafn;
        this.vidhengi = vidhengi;
        this.kennari_id_buidtil = kennari_id;
        this.dagur_buidtil = date;
        this.contenttype = contenttype;
    }

    public static Vidhengitafla create(int spurning_id, String nafn, File vidhengi, String kennari_id, String contenttype) throws SQLException {
        Date date = new Date();
        byte[] fileContent = new byte[(int) vidhengi.length()];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            FileInputStream fis = new FileInputStream(vidhengi.getPath());
            for (int readNum; (readNum = fis.read(fileContent)) != -1;) {
                bos.write(fileContent, 0, readNum); //no doubt here is 0
                //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
            }
        }
        catch (IOException ex) {;
        }

        Vidhengitafla vidhengitafla = new Vidhengitafla(spurning_id, nafn, bos.toByteArray(), kennari_id, date,contenttype);
        vidhengitafla.save();
        return vidhengitafla;
    }

    public static Finder<Integer, Vidhengitafla> find = new Finder<Integer, Vidhengitafla>(
            Integer.class, Vidhengitafla.class
    );
    public static List<Vidhengitafla> getAllVidhengi(){
        return Vidhengitafla.find.all();
    }

    public static byte[] getVidhengi(int spurning_id){
        byte[] vidhengi = Vidhengitafla.find.where().eq("spurning_id", spurning_id).findUnique().vidhengi;
        return vidhengi;
    }
    public static Integer createId(){
        List<Object> vidhengi = Vidhengitafla.find.findIds();

        Integer max = 1;
        for(Object verk : vidhengi){
            if(Integer.valueOf(verk.toString()) > max) {
                max = Integer.valueOf(verk.toString());
            }
        }
        return (max+1);
    }

    public static boolean doesVidhengiExist(int spurning_id){
        Vidhengitafla vidhengi = Vidhengitafla.find.where().eq("spurning_id", spurning_id).findUnique();
        if(vidhengi == null){
            return false;
        }
        else{
            return true;
        }
    }
    public static Vidhengitafla getVidhengiAll(int spurning_id){
        return Vidhengitafla.find.where().eq("spurning_id",spurning_id).findUnique();
    }

    public int getVidhengi_id() {
        return vidhengi_id;
    }

    public void setVidhengi_id(int vidhengi_id) {
        this.vidhengi_id = vidhengi_id;
    }

    public int getSpurning_id() {
        return spurning_id;
    }

    public void setSpurning_id(int spurning_id) {
        this.spurning_id = spurning_id;
    }

    public String getNafn() {
        return nafn;
    }

    public void setNafn(String nafn) {
        this.nafn = nafn;
    }

    public byte[] getVidhengiVariable() {
        return vidhengi;
    }

    public void setVidhengi(byte[] vidhengi) {
        this.vidhengi = vidhengi;
    }

    public Date getDagur_buidtil() {
        return dagur_buidtil;
    }

    public void setDagur_buidtil(Date dagur_buidtil) {
        this.dagur_buidtil = dagur_buidtil;
    }

    public String getKennari_id_buidtil() {
        return kennari_id_buidtil;
    }

    public void setKennari_id_buidtil(String kennari_id_buidtil) {
        this.kennari_id_buidtil = kennari_id_buidtil;
    }

    public String getContenttype() {
        return contenttype;
    }

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;
    }
}
